#ifndef __BIT_MANIP_H__
#define __BIT_MANIP_H__
#include <iostream>
#include <string>

using namespace std;

// Replace the bit from 'start' to 'end' in 'n' with that in 'm'
unsigned int replaceIntegerBits(unsigned int n,
								unsigned int m,
								unsigned int start, 
								unsigned int end);

// Print binary format of numbers
bool printNumberBinaries(const char *num_str, 
                         string &bin_str);

bool calcIntWithSameBitOnes(unsigned int val, 
                            unsigned int &smallest_larger,
                            unsigned int &biggest_smaller);

unsigned calcBitDistance(unsigned int val1, 
                         unsigned int val2);

unsigned swapBits(unsigned int val);

// Print a number in the binary format
template <class Type>
void printBits(Type val) {
    size_t bit_num = sizeof(Type) * 8;
    bool print_zero_bit = false;
    int tmp_val;
    int i;
    cout << "Total bits: " << bit_num << ", value: ";
    for (i = 0; i < bit_num; i++) {
        tmp_val = (int)((val >> (bit_num - 1 - i)) & 1);
        if (print_zero_bit == true || tmp_val == 1) {
            cout << tmp_val;
            print_zero_bit = true;
        }
    }
    cout << endl;

    return;
}


#endif

